﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace converter.template.item
{
    internal class Image : TemplateItem
    {
        public Image(string name)
        {
            this.name = name;
        }

        //public override void Build(Template template, Document document)
        //{
        //    ElementPict picture = CreatePicture(template, document);
        //    if (picture != null)
        //    {
        //        document.addBody(new ElementPara(document));
        //        document.addBody(picture);
        //    }
        //}

        //public override void Build(Template template, Document document, ElementCell cell)
        //{
        //    ElementPict picture = CreatePicture(template, document);
        //    if (picture != null)
        //    {
        //        cell.addBody(new ElementPara(document));
        //        cell.addBody(picture);
        //    }
        //}

        //private ElementPict CreatePicture(Template template, Document document)
        //{
        //    ElementPict picture = null;

        //    EmbeddedImage image = template.GetEmbeddedImage(name);
        //    if (image != null)
        //    {
        //        int imageType = GetImageType(image.GetMimeType());
        //        if (imageType != HtmlImage.BITMAP_UNKNOWN)
        //        {
        //            string fileName = image.GetFileName();
        //            DocHeader header = document.getHeader();

        //            picture = new ElementPict();
        //            int index = header.addPicture(fileName, imageType, image.GetData());
        //            picture.setImageIndex(index);
        //            picture.setNum(header.getNumPictures());
        //            picture.setName(name);
        //            picture.setPixelWidth(image.GetWidth());
        //            picture.setPixelHeight(image.GetHeight());
        //            picture.setDisplayWidth(PixelsToTwips(image.GetWidth()));
        //            picture.setDisplayHeight(PixelsToTwips(image.GetHeight()));
        //            picture.setWrap(ElementAbsPosition.WRAP_INLINE);
        //            picture.setLegit(true);
        //        }
        //    }

        //    return picture;
        //}

        //private static int GetImageType(string mimeType)
        //{
        //    // Returns one of the HtmlImage.BITMAP_* constant based on the mimeType
        //    switch (mimeType)
        //    {
        //        case "image/bmp":
        //            return HtmlImage.BITMAP_BMP;
        //        case "image/jpeg":
        //            return HtmlImage.BITMAP_JPG;
        //        case "image/gif":
        //            return HtmlImage.BITMAP_GIF;
        //        case "image/png":
        //            return HtmlImage.BITMAP_PNG;

        //        default:
        //            return HtmlImage.BITMAP_UNKNOWN;
        //    }
        //}

        private static int PixelsToTwips(int pixels)
        {
            const int TwipsPerPixel = 15;
            return pixels * TwipsPerPixel;
        }

        private readonly string name;
    }
}
